const db = uniCloud.database();
const userInfo = require('../user/user-info');
module.exports = async function(options = {}) {
	const { characterId, type, uid } = options;
	const token = this.getUniIdToken();
	if (!token || !uid || !characterId) {
		throw new Error('非法访问');
	}
	const user_info = await userInfo({ token });
	if (user_info.code == 0) { //获取有效用户信息
		let { _id: uid, character = [] } = user_info.data;
		character = character.filter(item => item != characterId);
		if (type === 'STUDENT') { // 学生角色的删除
			await Promise.all([
				db.collection('uni-id-users').doc(uid).update({ character }),
				db.collection('a-dz-character-list').where({ characterId }).remove(),
				db.collection('a-dz-complete-list').where({ characterId }).remove(),
				db.collection('a-dz-complete-list-custom').where({ characterId }).remove(),
				db.collection('a-dz-complete-stats-list').where({ characterId }).remove()
			])
		} else { // 老师角色的删除
			await Promise.all([
				db.collection('uni-id-users').doc(uid).update({ character }),
				db.collection('a-dz-character-list').where({ characterId }).remove(),
				db.collection('a-dz-group-list').where({ characterId }).remove()
			])
		}
		return {
			code: 0,
			msg: '删除成功'
		}
	} else {
		return user_info;
	}
}